//
//  MantuPlayApp.m
//  MantuPlay
//

#import "MantuPlayApp.h"
#import "TableViewContorollerSectionedByAlphabets.h"
#import "TracksDataSource.h"
#import "AlbumsDataSource.h"
#import "ArtistsDataSource.h"
#import "GenresDataSource.h"

@implementation MantuPlayApp

@synthesize window;

static NSOperationQueue *sharedOperationQueue = nil;

- (UINavigationController *)createNavigationControllerForDataSourceOfClass:(Class)datasourceClass {
	
	id<DatabaseObjectDataSource> dataSource = [[datasourceClass alloc] init];
	
	TableViewContorollerSectionedByAlphabets *theViewController = [[TableViewContorollerSectionedByAlphabets alloc] initWithDataSource:dataSource];

	[dataSource release];
	
	UINavigationController *theNavigationController;
	theNavigationController = [[UINavigationController alloc] initWithRootViewController:theViewController];

	[theViewController release];
	
	return theNavigationController;
}

- (void)applicationDidFinishLaunching:(UIApplication *)application {	
	sharedOperationQueue = [[NSOperationQueue alloc] init];
    [sharedOperationQueue setMaxConcurrentOperationCount:1];

	// Create window
	window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
	
	UINavigationController *navigationController;
	NSMutableArray *navigationControllerArray = [[NSMutableArray alloc] initWithCapacity:4];
    
	//Filesystem view (UITableViewController)
	/*filesystemView = [[FilesystemView alloc] initWithDirectory:[@"~/" stringByExpandingTildeInPath]];
	navigationController = [[UINavigationController alloc] initWithRootViewController:filesystemView];
	[navigationControllerArray addObject:navigationController];*/
	
	navigationController = [self createNavigationControllerForDataSourceOfClass:[TracksDataSource class]];
	[navigationControllerArray addObject:navigationController];
	[navigationController release];
	
	navigationController = [self createNavigationControllerForDataSourceOfClass:[AlbumsDataSource class]];
	[navigationControllerArray addObject:navigationController];
	[navigationController release];
	
	navigationController = [self createNavigationControllerForDataSourceOfClass:[ArtistsDataSource class]];
	[navigationControllerArray addObject:navigationController];
	[navigationController release];
	
	navigationController = [self createNavigationControllerForDataSourceOfClass:[GenresDataSource class]];
	[navigationControllerArray addObject:navigationController];
	[navigationController release];
	
	tabBar = [[UITabBarController alloc] init];
	tabBar.viewControllers = navigationControllerArray;
	
	[navigationController release];
	[navigationControllerArray release];
	
	// Show window
	[window addSubview:tabBar.view];
	[window makeKeyAndVisible];
}

-(NSOperationQueue*)sharedOperationQueue {
	return sharedOperationQueue;
}

/*- (void)applicationSuspend:(struct __GSEvent *)evt
{
}*/

- (void)dealloc {
	[tabBar release];
	[window release];
	[sharedOperationQueue release];
	[super dealloc];
}

@end
